package com.herui.ota.config;

import com.herui.ota.exception.ServiceException;
import com.herui.ota.service.UserService;
import com.herui.ota.service.iml.LightUserAccountServiceIml;
import com.mlnx.common.utils.MyLog;
import org.apache.commons.lang3.StringUtils;
import org.shan.base.layer.user.entity.UserAccount;
import org.shan.base.layer.user.service.UserAccountService;
import org.shan.security.app.authorize.AppAuthorizeManager;
import org.shan.security.app.authorize.MlnxOauthUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.common.util.OAuth2Utils;
import org.springframework.security.oauth2.provider.ClientDetails;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * Created by amanda.shan on 2019/6/20.
 */
@Component
public class ShopAuthorizeManager implements AppAuthorizeManager {

    MyLog log = MyLog.getLog(getClass());

    @Autowired
    private UserService userService;

    @Autowired
    private LightUserAccountServiceIml userAccountService;

    @Autowired
    private PasswordEncoder encoder;

    @Override
    public MlnxOauthUser authenticate(Map<String, String> requestParameters, ClientDetails authenticatedClient) {

        String grantType = requestParameters.get(OAuth2Utils.GRANT_TYPE);

        // 刷新Token不需要添加用户名密码
        if (grantType.equals("refresh_token")) {
            return null;
        }

        // 微信code 登入
        String code = requestParameters.get("code");
        String appid = requestParameters.get("appid");
//        String wxMaUserInfo = requestParameters.get("wxMaUserInfo");
//        String rawData = requestParameters.get("rawData");
//        String signature = requestParameters.get("signature");
//        String encryptedData = requestParameters.get("encryptedData");
//        String iv = requestParameters.get("iv");

        String wxOpenid = requestParameters.get("wxOpenid");

        // 管理员登入
        String username = requestParameters.get("username");
        String password = requestParameters.get("password");

        // 根据获取用户信息
        UserAccount userAccount = null;

        if (!StringUtils.isEmpty(wxOpenid)) {
            userAccount = userAccountService.findByUsername(wxOpenid);
        } else if (!StringUtils.isEmpty(code)) {
            throw new ServiceException("不支持微信登入");
        } else if (!StringUtils.isEmpty(username) && !StringUtils.isEmpty(password)) {
            userAccount = userAccountService.findByUsername(username);

            if (userAccount != null && userAccountService.isLock(userAccount.getId())){
                throw new ServiceException("账户已经被锁定，请联系管理员");
            }

            if (userAccount == null || !encoder.matches(password, userAccount.getPassword())) {
                if (userAccount != null){
                    userAccountService.loginFail(userAccount.getId());
                }
                throw new ServiceException("用户名密码错误");
            }
        } else {
            throw new ServiceException("微信code 不能为空");
        }

        if (userAccount != null) {
            password = userAccount.getPassword();
            if (StringUtils.isEmpty(password)) {
                password = "123456";
            }
            return new MlnxOauthUser(userAccount.getUsername(), password);
        } else {
            throw new ServiceException("微信code码错误");
        }
    }
}
